package com.boot.security.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.boot.security.server.mapper.MBranchMapper;
import com.boot.security.server.model.domain.MBranch;
import com.boot.security.server.model.dto.BranchDto;
import com.boot.security.server.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.BranchDao;
import com.boot.security.server.model.Branch;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/branchs")
public class BranchController {
    @Autowired
    private MBranchMapper branchMapper;

    @Autowired
    private BranchDao branchDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public Branch save(@RequestBody Branch branch) {
        branchDao.save(branch);

        return branch;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public Branch get(@PathVariable Long id) {
        return branchDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public Branch update(@RequestBody Branch branch) {
        branchDao.update(branch);

        return branch;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return branchDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<Branch> list(PageTableRequest request) {
                return branchDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        branchDao.delete(id);
    }

    @GetMapping("/getList")
    @ApiOperation(value = "列表")
    public List<BranchDto> getList() {

        List<MBranch> branchs = branchMapper.selectAll();
        List<BranchDto> branchDtos = BeanHelper.copyWithCollection(branchs, BranchDto.class);
        for (BranchDto branchDto : branchDtos) {
            MBranch branch = new MBranch();
            branch.setParentId(branchDto.getId());
            List<MBranch> branches = branchMapper.select(branch);
            List<BranchDto> branchDtoList = BeanHelper.copyWithCollection(branches, BranchDto.class);
            branchDto.setBranchDtos(branchDtoList);
        }
        return branchDtos;
    }

    @PostMapping("/getBranchs")
    @ApiOperation(value = "列表")
    public List<MBranch> getList(@RequestParam(value = "brandId", required = false) Integer brandId) {
        return branchMapper.selectAll();
    }

    @GetMapping("/getBranchList")
    @ApiOperation(value = "列表")
    public Map<String, Object> getBranchList() {
        List<MBranch> branches = branchMapper.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", branches.size());
        map.put("data", branches);
        return map;
    }
}
